using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using SVNClean.Modules;
using SVNClean.Properties;
using Keyrox.Shared.Controls;
using DevExpress.XtraBars;

namespace SVNClean.Windows {
    public partial class frm_Main : DevExpress.XtraEditors.XtraForm {
        /// <summary>
        /// Initializes a new instance of the <see cref="frm_Main"/> class.
        /// </summary>
        public frm_Main() {
            InitializeComponent();
            InitializeSkinControls();
            InitializeShowOptions();
            
            ctrl_Explorer1.DirectoryChanged += new EventHandler<SVNClean.Modules.DirectoryEventArgs>(ctrl_Explorer1_DirectoryChanged);
            SetDefaultSkin(AppConfiguration.Current.DefaultSkin, false);

            btnBackup.Down = AppConfiguration.Current.CreateBackup;
            btnLog.Down = AppConfiguration.Current.SaveOperationLog;
        }

        private DirectoryInfo directoryToClean;
        /// <summary>
        /// Gets or sets the directory to clean.
        /// </summary>
        /// <value>The directory to clean.</value>
        private DirectoryInfo DirectoryToClean {
            get { return directoryToClean; }
            set {
                directoryToClean = value;
                if (value != null) { FixToolTip(btnClearNow); }
            }
        }

        /// <summary>
        /// Handles the Load event of the frm_Main control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void frm_Main_Load(object sender, EventArgs e) {
            ctrl_Explorer1.InitData();
            ctrl_Explorer1.SelectLastDirectory();
            btnVersion.Caption = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
        }

        /// <summary>
        /// Initializes the show options.
        /// </summary>
        private void InitializeShowOptions() {
            bckShowHiddenFiles.Checked = AppConfiguration.Current.ShowHiddenFiles;
            bckShowFiles.Checked = AppConfiguration.Current.ShowFiles;
            bckShowHiddenFolders.Checked = AppConfiguration.Current.ShowHiddenFolders;
        }

        #region "[rgn] Control Events "
        private void ctrl_Explorer1_DirectoryChanged(object sender, SVNClean.Modules.DirectoryEventArgs e) {
            DirectoryToClean = e.Directory;
        }
        private void btnClean_Click(object sender, EventArgs e) {
            if (DirectoryToClean != null) {
                PerformCleanActions();
            }
        }
        private void btnSvnNames_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
            this.Hide();
            var svnNames = new frm_SvnNames();
            svnNames.ShowDialog();
            this.Show();
        }
        private void btnAbout_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
            this.Hide();
            var about = new frm_About();
            about.ShowDialog();
            this.Show();
        }
        private void btnAuthor_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
            this.Hide();
            var about = new frm_Author();
            about.ShowDialog();
            this.Show();
        }
        private void btnHelp_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
            var info = new ProcessStartInfo("iexplore.exe");
            info.Arguments = Resources.SupportURL;
            Process.Start(info);
        }
        private void btnExit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
            Application.Exit();
        }
        private void barButtonItem5_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
            this.Hide();
            var about = new frm_About();
            about.ShowDialog();
            this.Show();
        }
        private void btnConfig_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
            try {
                this.Hide();
                var config = new frm_Preferences();
                config.ShowDialog();
                this.Show();

                SetDefaultSkin(AppConfiguration.Current.DefaultSkin, false);
                btnBackup.Down = AppConfiguration.Current.CreateBackup;
                btnLog.Down = AppConfiguration.Current.SaveOperationLog;
                InitializeShowOptions();
            }
            catch (Exception) { Debugger.Break(); }
        }
        private void btnVersion_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
            this.Hide();
            var about = new frm_About();
            about.ShowDialog();
            this.Show();
        }
        private void barButtonItem7_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
            btnAuthor_ItemClick(this, e);
        }
        private void FixToolTip(BarLargeButtonItem control) {

            var superToolTip1 = new DevExpress.Utils.SuperToolTip();
            var toolTipTitleItem1 = new DevExpress.Utils.ToolTipTitleItem();
            var toolTipItem1 = new DevExpress.Utils.ToolTipItem();
            var toolTipSeparatorItem1 = new DevExpress.Utils.ToolTipSeparatorItem();
            var toolTipTitleItem2 = new DevExpress.Utils.ToolTipTitleItem();

            toolTipTitleItem1.Appearance.Image = global::SVNClean.Properties.Resources.bug_blue_32;
            toolTipTitleItem1.Appearance.Options.UseImage = true;
            toolTipTitleItem1.Image = global::SVNClean.Properties.Resources.bug_blue_32;
            toolTipTitleItem1.Text = Resources.ClearDirectory;
            toolTipItem1.LeftIndent = 6;
            toolTipItem1.Text = string.Format(Resources.PerformCleanIn, directoryToClean.FullName);
            toolTipTitleItem2.LeftIndent = 6;
            toolTipTitleItem2.Text = Resources.UseBackup;
            superToolTip1.Items.Add(toolTipTitleItem1);
            superToolTip1.Items.Add(toolTipItem1);
            superToolTip1.Items.Add(toolTipSeparatorItem1);
            superToolTip1.Items.Add(toolTipTitleItem2);
            control.SuperTip = superToolTip1;
        }
        private void bckShowFiles_ItemClick(object sender, ItemClickEventArgs e) {
            AppConfiguration.Current.ShowFiles = bckShowFiles.Checked;
            AppConfiguration.Current.SaveData();
            ctrl_Explorer1.InitData();
        }
        private void bckShowHiddenFiles_ItemClick(object sender, ItemClickEventArgs e) {
            AppConfiguration.Current.ShowHiddenFiles = bckShowHiddenFiles.Checked;
            AppConfiguration.Current.SaveData();
            ctrl_Explorer1.InitData();
        }
        private void bckShowHiddenFolders_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
            AppConfiguration.Current.ShowHiddenFolders = bckShowHiddenFolders.Checked;
            AppConfiguration.Current.SaveData();
            ctrl_Explorer1.InitData();
        }
        private void btnBackup_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
            AppConfiguration.Current.CreateBackup = btnBackup.Down;
            AppConfiguration.Current.SaveData();
        }
        private void btnLog_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
            AppConfiguration.Current.SaveOperationLog = btnLog.Down;
            AppConfiguration.Current.SaveData();
        }
        private void btnClearNow_ItemClick(object sender, ItemClickEventArgs e) {
            if (DirectoryToClean != null) {
                PerformCleanActions();
            }
        }
        #endregion

        #region "[rgn] Skin Events    "
        List<DevExpress.XtraBars.BarCheckItem> SkinButtons = new List<DevExpress.XtraBars.BarCheckItem>();
        private bool updatingCheckState = false;
        private void InitializeSkinControls() {
            SkinButtons = new List<DevExpress.XtraBars.BarCheckItem>{
                btnBlueSkin, btnBlackSkin, btnXmasSkin,
                btnSkySkin, btnValantineSkin, btnMacSkin };

            foreach (var btn in SkinButtons) {
                btn.CheckedChanged += btnSkin_CheckedChanged;
                if (AppConfiguration.Current.DefaultSkin == btn.Tag.ToString()) {
                    btn.Checked = true;
                }
                else { btn.Checked = false; }
            }
        }
        private void btnSkin_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
            if (!updatingCheckState) {
                if (e.Item.Tag.ToString() != AppConfiguration.Current.DefaultSkin) {
                    SetDefaultSkin(e.Item.Tag.ToString(), true);
                }
            }
        }
        private void UncheckSkinButtons(DevExpress.XtraBars.BarItem btnchecked) {
            if (btnchecked.Tag != null) UncheckSkinButtons(btnchecked.Tag.ToString());
        }
        private void UncheckSkinButtons(string skinName) {
            updatingCheckState = true;
            foreach (var btn in SkinButtons) {
                if (btn.Tag.ToString().ToLower() != skinName.ToLower()) btn.Checked = false;
                else btn.Checked = true;
            }
            updatingCheckState = false;
        }
        #endregion

        /// <summary>
        /// Performs the clean actions.
        /// </summary>
        public void PerformCleanActions() {
            this.Hide();
            var frmClean = new frm_Clean { DirectoryToClean = DirectoryToClean };
            frmClean.ShowDialog();

            UpdateFolderList();
            this.Show();
        }

        /// <summary>
        /// Updates the folder list.
        /// </summary>
        public void UpdateFolderList() {
            string lastDirectory = string.Empty;
            if (DirectoryToClean != null) lastDirectory = DirectoryToClean.GetDirectoryPath();
            else lastDirectory = Directory.GetLogicalDrives()[0];

            ctrl_Explorer1.InitData();
            ctrl_Explorer1.SelectDirectory(lastDirectory);
        }

        /// <summary>
        /// Sets the default skin.
        /// </summary>
        /// <param name="skinName">Name of the skin.</param>
        /// <param name="save">if set to <c>true</c> [save].</param>
        public void SetDefaultSkin(string skinName, bool save) {
            if (defaultLookAndFeel1.LookAndFeel.SkinName != skinName) {
                DevExpress.LookAndFeel.UserLookAndFeel.Default.SetSkinStyle(skinName);
                this.defaultLookAndFeel1.LookAndFeel.SetSkinStyle(skinName);
                this.Refresh(); this.Update();
                UncheckSkinButtons(skinName);
                if (save) {
                    AppConfiguration.Current.DefaultSkin = skinName;
                    AppConfiguration.Current.SaveData();
                }
            }
        }

    }
}